package com.quectel.business.common.file;

import cn.hutool.core.io.FileMagicNumber;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ArrayUtil;
import com.quectel.core.module.file.vo.FileDataVO;
import com.quectel.util.common.ImageUtils;
import com.quectel.util.common.Response;
import com.quectel.util.kit.StorageKit;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * @author zhangheng
 * @email nolan.zhang@quectel.com
 * @date 2023/7/17 9:21
 * @description new java file header...
 */
@Component
public class FileBusiness {

    /**
     * 保存文件为临时文件
     *
     * @param fileList
     * @param acceptType
     * @return
     */
    public Response<List<String>> handleFileToTempPaths(MultipartFile[] fileList, Set<String> acceptType) {
        if (ArrayUtil.isEmpty(fileList)) {
            return Response.<List<String>>ok().wrap(Collections.emptyList());
        }
        List<String> filePaths = new ArrayList<>();
        //处理图片文件
        for (MultipartFile file : fileList) {
            File tempFile = null;
            try {
                String contentType = file.getContentType();
                FileMagicNumber magicNumber = FileMagicNumber.getMagicNumber(file.getBytes());
                if (contentType == null || FileMagicNumber.UNKNOWN.equals(magicNumber)) {
                    return Response.error("不支持的图片格式");
                }
                if (!acceptType.contains(magicNumber.getMimeType())) {
                    return Response.error("不支持的图片格式");
                }
                tempFile = ImageUtils.compressImgWriteToTempDir(file.getInputStream(), file.getOriginalFilename(), false);
                filePaths.add(tempFile.getPath());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return Response.<List<String>>ok().wrap(filePaths);
    }

    /**
     * 上传文件到miniIO ,删除临时文件
     *
     * @param filePaths
     * @return
     */
    public Response<List<String>> batchUploadFile(List<String> filePaths) {
        if (CollectionUtils.isEmpty(filePaths)) {
            return Response.<List<String>>ok().wrap(Collections.emptyList());
        }
        List<String> urlList = new ArrayList<>();
        for (String imgFilePath : filePaths) {
            File tempFile = null;
            try {
                tempFile = new File(imgFilePath);
                String url = StorageKit.uploadToPublicBucketObtainUrl(tempFile);
                urlList.add(url);
            } catch (RuntimeException e) {
                e.printStackTrace();
                return Response.error(e.getMessage());
            } finally {
                if (tempFile != null && tempFile.exists()) {
                    FileUtil.del(tempFile);
                }
            }

        }
        return Response.<List<String>>ok().wrap(urlList);
    }

    /**
     * 上传文件到miniIO ,删除临时文件
     *
     * @param filePaths
     * @return
     */
    public Response<List<FileDataVO>> batchUploadFileAndGetFileData(List<String> filePaths) {
        if (CollectionUtils.isEmpty(filePaths)) {
            return Response.<List<FileDataVO>>ok().wrap(Collections.emptyList());
        }
        List<FileDataVO> fileDataList = new ArrayList<>();
        for (String imgFilePath : filePaths) {
            File tempFile = null;
            try {
                tempFile = new File(imgFilePath);
                String url = StorageKit.uploadToPublicBucketObtainUrl(tempFile);
                FileDataVO fileData = new FileDataVO();
                fileData.setName(tempFile.getName());
                fileData.setUrl(url);
                fileDataList.add(fileData);
            } catch (RuntimeException e) {
                e.printStackTrace();
                return Response.error(e.getMessage());
            } finally {
                if (tempFile != null && tempFile.exists()) {
                    FileUtil.del(tempFile);
                }
            }

        }
        return Response.<List<FileDataVO>>ok().wrap(fileDataList);
    }
}
